package com.liujit.generator.engine;

import com.liujit.generator.engine.base.AbstractTemplateEngine;
import com.liujit.generator.utils.ToolUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author: liujun
 * @date: 2021/6/23 10:13 上午
 * @description: 通用的模板生成引擎
 */
@Slf4j
public class TemplateEngine extends AbstractTemplateEngine {

    @Override
    protected void generateController() {
        String path = this.getPath(super.getControllerConfig().getTemplatePath());
        generateFile(super.getContextConfig().getTemplatePrefixPath() + "/Controller.java.btl", path);
        log.info(super.getContextConfig().getBizEnBigName() + "Controller生成成功!");
    }

    @Override
    protected void generateService() {
        String path = this.getPath(super.getServiceConfig().getTemplatePath());
        generateFile(super.getContextConfig().getTemplatePrefixPath() + "/Service.java.btl", path);
        log.info(super.getContextConfig().getBizEnBigName() + "Service生成成功!");
        String implPath = this.getPath(super.getServiceConfig().getImplTemplatePath());
        generateFile(super.getContextConfig().getTemplatePrefixPath() + "/ServiceImpl.java.btl", implPath);
        log.info(super.getContextConfig().getBizEnBigName() + "ServiceImpl生成成功!");
    }

    @Override
    protected void generateMapper() {
        String path = this.getPath(super.getMapperConfig().getTemplatePath());
        generateFile(super.getContextConfig().getTemplatePrefixPath() + "/Mapper.java.btl", path);
        log.info(super.getContextConfig().getBizEnBigName() + "Mapper生成成功!");
    }

    @Override
    protected void generateModel() {
        String path = this.getPath(super.getModelConfig().getTemplatePath());
        generateFile(super.getContextConfig().getTemplatePrefixPath() + "/Model.java.btl", path);
        log.info(super.getContextConfig().getBizEnBigName() + "Model生成成功!");
    }

    @Override
    protected void generateQuery() {
        String path = this.getPath(super.getQueryConfig().getTemplatePath());
        generateFile(super.getContextConfig().getTemplatePrefixPath() + "/Query.java.btl", path);
        log.info(super.getContextConfig().getBizEnBigName() + "Query生成成功!");
    }

    @Override
    protected void generateParams() {
        String path = this.getPath(super.getParamsConfig().getTemplatePath());
        generateFile(super.getContextConfig().getTemplatePrefixPath() + "/Params.java.btl", path);
        log.info(super.getContextConfig().getBizEnBigName() + "Params生成成功!");
    }

    @Override
    protected void generateResult() {
        String path = this.getPath(super.getResultConfig().getTemplatePath());
        generateFile(super.getContextConfig().getTemplatePrefixPath() + "/Result.java.btl", path);
        log.info(super.getContextConfig().getBizEnBigName() + "Result生成成功!");
    }

    /**
     * 获取文件生成路径
     *
     * @param path
     * @return
     */
    private String getPath(String path) {
        return ToolUtil
                .format(super.getContextConfig().getProjectPath() + path, super.getContextConfig().getProjectName(),
                        super.getContextConfig().getProjectName(), super.getContextConfig().getBizEnBigName());
    }
}
